<?php

namespace App\Http\Controllers\ClubLeader;

use App\Club;
use App\ClubVideos;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\Facades\Image;

class ClubVideoController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $cvideos = ClubVideos::latest('created_at')->Paginate(8);
        return view('ClubLeader.clubVideo', compact('cvideos'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('ClubLeader.createClubVideo');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $club_id = Club::latest('created_at')->first()->value('id');

        $rules = [
            'cvideo_name' => 'required',
            'cvideo_pic' => 'required',
            'savedpath' => 'required'
        ];
        $this -> validate($request, $rules);
        $this -> uploadAvatar($request);
        $clubVideoPic = $request -> get('cvideo_name')
            . '.' . $request -> file('cvideo_pic')
                -> getClientOriginalExtension();

        $clubVideo = new ClubVideos;
        $clubVideo -> cvideo_pic = $clubVideoPic;
        $clubVideo -> cvideo_address = $request -> get('savedpath');
        $clubVideo -> cvideo_name = $request -> get('cvideo_name');
        $clubVideo -> club_id = $club_id;
        $clubVideo -> save();

        session() -> flash('success', '俱乐部视频信息添加成功');

        return redirect() -> route('clubVideo.index');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $video = ClubVideos::find($id);
        $url = Storage::disk('videos')->url($cvideo->cvideo_address);
        return $url;
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $clubVideo = ClubVideos::findOrFail($id);
        $pic = $clubVideo -> cvideo_pic;
        $video = $clubVideo -> cvideo_address;
        if ($pic != 'default.jpg') {
            Storage::disk('clubs_video') -> exists($pic);
            Storage::disk('clubs_video') -> delete($pic);
            Storage::disk('videos') -> delete($video);
        }
        $clubVideo -> delete();

        session() -> flash('success', '删除成功');

        return redirect() -> route('clubVideo.index');
    }

    /**
     * Upload Avatar to public_path
     * @param Request $request
     */
     public function uploadAvatar(Request $request)
     {
         if ($request->isMethod('post') OR $request->isMethod('patch')) {

             $pic = $request->file('cvideo_pic');

             if ($pic->isValid()) {

                 $realPath = $pic->getRealPath();
                 $ext = $pic->getClientOriginalExtension();
                 $masterName = $request->get('cvideo_name');
                 $fileName = $masterName. '.' .$ext;
                 Image::make($pic)->fit(300, 300)->save(public_path('/uploads/clubs_video/videos/') . $fileName);
             }
         }
     }
}
